/*
 * Copyright 2010-2011 Mike Novak <michael.novakjr@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.codeversed.plugins.buzz.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.codeversed.plugins.buzz.R;
import com.codeversed.plugins.buzz.util.Constants;

public class GalleryAdapter extends BaseAdapter {
    private final Context mContext;
    private Bitmap[] mImages;
    
    private int mDimension;
    private int mGalleryItemBackground;
    
    public GalleryAdapter(Context context) {
        mContext = context;
        mDimension = 150 * new Float(context.getResources().getDisplayMetrics().density).intValue();
        
        TypedArray a = context.obtainStyledAttributes(R.styleable.PhotoGallery);
        mGalleryItemBackground = a.getResourceId(R.styleable.PhotoGallery_android_galleryItemBackground, 0);
        a.recycle();
    }
    
    public void setImages(Bitmap[] images) {
        mImages = images;
    }
    
    public int getCount() {
        return mImages.length;
    }
    
    public Object getItem(int position) {
        return mImages[position];
    }
    
    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(Constants.LOG_TAG, "Dimension: " + mDimension);
        
        ImageView view = new ImageView(mContext);
        view.setImageBitmap(mImages[position]);
        view.setLayoutParams(new Gallery.LayoutParams(mDimension, mDimension));
        view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        view.setBackgroundResource(mGalleryItemBackground);
        
        return view;
    }
}
